#!/bin/bash
currentNodeDomain=$1
trustStore=$2
currentNodeKeyStore=$3
currentNodeAlias=$4
trustStoreDir=`dirname $trustStore`

if [ ! -f "$trustStoreDir/$currentNodeDomain.cer" ]; then
cd $trustStoreDir
keytool -export -alias $currentNodeAlias -storepass 123456 -file $currentNodeDomain.cer -keystore $currentNodeKeyStore
fi;

if [ $(keytool -list   -keystore $trustStore -storepass 123456 | grep -c $currentNodeDomain) -eq  0 ]; then
/usr/bin/expect<<EOF
spawn keytool -import -alias $currentNodeDomain -file $trustStoreDir/$currentNodeDomain.cer -keystore $trustStore -trustcacerts -storepass 123456  -keypass 123456
expect "]:"
send "y\r"
interact
EOF
fi;

